package domain.valueObejcts;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;

import ui.custom.panel.MonthTabs;
import ui.custom.panel.PersonalPanel;

@Entity
@SequenceGenerator(name = "project_seq", sequenceName = "project_id_seq")
public class Project implements Serializable{

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
	private String month;
	private String year;
	private String name;
	private Date creationDate;
	private Date lastModified;
	private transient MonthTabs tabs;
	private transient PersonalPanel personalPanel;
	@OneToMany(mappedBy = "project")
	private List<ProjectPosition> positions;

	public Project(){
		this.positions = new Vector<ProjectPosition>();
		this.creationDate = new Date();
	}
	
	public Project(String month, String year, String name){
		this.positions = new Vector<ProjectPosition>();
		this.creationDate = new Date();
		this.lastModified = new Date();
		this.month = month;
		this.year = year;
		this.name = name;
	}

	public Project(String name, MonthTabs monthTabs, PersonalPanel personalPanel) {
		this.name = name;
		this.personalPanel = personalPanel;
		this.tabs = monthTabs;
		this.positions = new Vector<ProjectPosition>();
		this.creationDate = new Date();
		this.month = monthTabs.getMonth();
		this.year = monthTabs.getYear();
		this.lastModified = new Date();
	}

	public void setId(int id){
		this.id = id;
	}

	public int getId(){
		return this.id;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public PersonalPanel getPersonalPanel() {
		return this.personalPanel;
	}

	public MonthTabs getMonthTabs(){
		return this.tabs;
	}

	public void setName(String name){
		this.name = name;
	}

	public String getName(){
		return this.name;
	}

	public void setCreationDate(Date date){
		this.creationDate = date;
	}

	public Date getCreationDate(){
		return this.creationDate;
	}

	public void setLastModified(Date lastModified){
		this.lastModified = lastModified;
	}

	public Date getLastModified(){
		return this.lastModified;
	}

	public void setPositions(List<ProjectPosition> positions){
		this.positions = positions;
	}

	public List<ProjectPosition> getPositions(){
		return this.positions;
	}

	@Override
	public String toString() {
		return this.name;
	}

	public String getShifts(Employee employee) {
		int count = 0;
		for (ProjectPosition pos : this.positions) {
			if(pos.getEmployee().equals(employee)){
				count++;
			}
		}
		return count + "";
	}

	public String getWorked(Employee employee) {
		int minutesTotal = 0;
		for (ProjectPosition pos : this.positions) {
			if(pos.getEmployee().equals(employee)){
				minutesTotal += pos.getMinutesWorked();
			}
		}
		int hours = minutesTotal / 60;
		int minutes = minutesTotal % 60;
		return hours + "h" + minutes + "m";
	}

	public String getPlaned(Employee employee) {
		int minutesTotal = 0;
		for (ProjectPosition pos : this.positions) {
			if(pos.getEmployee().equals(employee)){
				minutesTotal += pos.getMinutesPlaned();
			}
		}
		int hours = minutesTotal / 60;
		int minutes = minutesTotal % 60;
		return hours + "h" + minutes + "m";
	}

	public void addProjectPosition(ProjectPosition pos) {
		this.positions.add(pos);
	}

	public ProjectPosition getPosition(Employee employee, String date, Location location) {
		for (ProjectPosition pos : this.positions) {
			if(pos.getEmployee().equals(employee) && pos.getDateString().equals(date) && pos.getLocation().equals(location)){
				return pos;
			}
		}
		return null;
	}

	public void updateMinutesPlaned(Location location, Employee employee, int minutes) {
		for (ProjectPosition pos : this.positions) {
			if(pos.getLocation().equals(location) && pos.getEmployee().equals(employee)){
				pos.setMinutesPlaned(minutes);
				break;
			}
		}
	}

	public void setMonthTabs(MonthTabs monthTabs) {
		this.tabs = monthTabs;
	}

	public void setPersonalPanel(PersonalPanel personalPanel) {
		this.personalPanel = personalPanel;
	}

	public void update(ProjectPosition pos) {
		for (ProjectPosition p : this.positions) {
			if(p.getId() == pos.getId()){
				p.setDate(pos.getDate());
				p.setDateString(pos.getDateString());
				p.setEmployee(pos.getEmployee());
				p.setFloor(pos.getFloor());
				p.setLocation(pos.getLocation());
				p.setMinutesPlaned(pos.getMinutesPlaned());
				p.setMinutesWorked(pos.getMinutesWorked());
				break;
			}
		}
	}




}
